package managedBean;

import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.HashMap;
import javax.annotation.Resource;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperRunManager;


@ManagedBean(name="JasperReportsManagedBean")
@RequestScoped

public class JasperReportsManagedBean 
{

 /*  private static class JasperParameter {

        public JasperParameter() {
        }
    }*/
    @Resource(name = "report")
    private DataSource report;
    
    private String localhost;
    
    
  
    public JasperReportsManagedBean()
    {
    }
    
    
    
    public void generateSalesLeadReport(ActionEvent event)
    {
        try
        {
            FacesContext ec = FacesContext.getCurrentInstance();
            
            String strSQL = "SELECT * FROM CLIENTENTITY C INNER JOIN SALESLEADENTITY S ON C.ID = S.ID";
            ResultSet rsAddresses = report.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsAddresses);
            InputStream reportStream = ((HttpSession)ec.getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/report/saleslead.jasper");
        //    JasperParameter jasperParameter;
        //    jasperParameter.put("IMAGEPATH", "http://" + "locahost:8181/is3102Web/report/merlionhomepage.png");
            
            ec.getExternalContext().setResponseContentType("application/pdf");
            OutputStream outputStream = ec.getExternalContext().getResponseOutputStream();
            
            JasperRunManager.runReportToPdfStream(reportStream, outputStream, new HashMap(), jrResultSetDataSource);
            
            outputStream.flush();
            outputStream.close();
          
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void generateCustomerReport(ActionEvent event)
    {
        try
        {
            FacesContext ec = FacesContext.getCurrentInstance();
            
            String strSQL = "SELECT * FROM CLIENTENTITY C INNER JOIN CUSTOMERENTITY CU ON C.ID = CU.ID";
            ResultSet rsAddresses = report.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsAddresses);
            InputStream reportStream = ((HttpSession)ec.getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/report/customer.jasper");
        //    JasperParameter jasperParameter;
        //    jasperParameter.put("IMAGEPATH", "http://" + "locahost:8080/is3102Web/report/merlionhomepage.png");
            
            ec.getExternalContext().setResponseContentType("application/pdf");
            OutputStream outputStream = ec.getExternalContext().getResponseOutputStream();
            
            JasperRunManager.runReportToPdfStream(reportStream, outputStream, new HashMap(), jrResultSetDataSource);
            
            outputStream.flush();
            outputStream.close();
            ec.responseComplete();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
}
    
    public void generateCustomerExecutiveReport(ActionEvent event)
    {
        try
        {
            FacesContext ec = FacesContext.getCurrentInstance();
            
            String strSQL = "SELECT * FROM CUSTOMEREXECUTIVEENTITY";
            ResultSet rsAddresses = report.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsAddresses);
            InputStream reportStream = ((HttpSession)ec.getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/report/customerExecutive.jasper");
        //    JasperParameter jasperParameter;
        //    jasperParameter.put("IMAGEPATH", "http://" + "locahost:8080/is3102Web/report/merlionhomepage.png");
            
            ec.getExternalContext().setResponseContentType("application/pdf");
            OutputStream outputStream = ec.getExternalContext().getResponseOutputStream();
            
            JasperRunManager.runReportToPdfStream(reportStream, outputStream, new HashMap(), jrResultSetDataSource);
            
            outputStream.flush();
            outputStream.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
}
    
      public void generateCreditProfileReport(ActionEvent event)
    {
        try
        {
            FacesContext ec = FacesContext.getCurrentInstance();
            
            String strSQL = "SELECT * FROM CREDITPROFILEENTITY CP INNER JOIN CLIENTENTITY C ON CP.CUSTOMERID = C.ID";
            ResultSet rsAddresses = report.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsAddresses);
            InputStream reportStream = ((HttpSession)ec.getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/report/creditProfile.jasper");
        //    JasperParameter jasperParameter;
        //    jasperParameter.put("IMAGEPATH", "http://" + "locahost:8080/is3102Web/report/merlionhomepage.png");
            
            ec.getExternalContext().setResponseContentType("application/pdf");
            OutputStream outputStream = ec.getExternalContext().getResponseOutputStream();
            
            JasperRunManager.runReportToPdfStream(reportStream, outputStream, new HashMap(), jrResultSetDataSource);
            
            outputStream.flush();
            outputStream.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    public void generateInvoice(ActionEvent event)
    {
        try
        {
            FacesContext ec = FacesContext.getCurrentInstance();
            
            HashMap parameter = new HashMap();
            parameter.put("billingStreet", "SELECT BILLINGSTREET FROM BILLINGADDRESSENTITY");
            parameter.put("billingCode", "SELECT BILLINGCODE FROM BILLINGADDRESSENTITY");
            parameter.put("billingState", "SELECT BILLINGSTATE FROM BILLINGADDRESSENTITY");
            parameter.put("billingCountry", "SELECT BILLINGCOUNTRY FROM BILLINGADDRESSENTITY");
            parameter.put("billingCity", "SELECT BILLINGCITY FROM BILLINGADDRESSENTITY");
            
            parameter.put("shippingStreet", "SELECT SHIPPINGSTREET FROM SHIPPINGADDRESSENTITY");
            parameter.put("shippingCode", "SELECT SHIPPINGCODE FROM SHIPPINGADDRESSENTITY");
            parameter.put("shippingState", "SELECT SHIPPINGSTATE FROM SHIPPPINGADDRESSENTITY");
            parameter.put("shippingCountry", "SELECT SHIPPINGCOUNTRY FROM SHIPPINGADDRESSENTITY");
            parameter.put("shippingStreet", "SELECT SHIPPINGSTREET FROM SHIPPINGADDRESSENTITY");
            
            String strSQL = "SELECT * FROM CREDITPROFILEENTITY CP INNER JOIN CLIENTENTITY C ON CP.CUSTOMERID = C.ID";
            ResultSet rsAddresses = report.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsAddresses);
            InputStream reportStream = ((HttpSession)ec.getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/report3/.jasper");
            
           /* 
            parameter.put("invoiceId", "SELECT INVOICEID FROM INVOICEANDRECEIPTENTITY");
            parameter.put("company", "SELECT NAME FROM CLIENTENTITY");
            parameter.put("issueDate", "SELECT ISSUEDATE FROM INVOICEANDRECEIPTENTITY");
            
            parameter.put("cashInAdvance", "SELECT CASHINADVANCE FROM INVOICEANDRECEIPTENTITY");
            parameter.put("amountDue", "SELECT AMOUNTDUE FROM INVOICEANDRECEIPTENTITY");
            parameter.put("discountRate", "SELECT DISCOUNTRATE FROM INVOICEANDRECEIPTENTITY");
            parameter.put("totalPrice", "SELECT TOTALPRICE FROM INVOICEANDRECEIPTENTITY");
            
            parameter.put("customerExName", "SELECT NAME FROM CUSTOMEREXECUTIVEENTITY");
            parameter.put("shippingDate", "SELECT NAME FROM CUSTOMEREXECUTIVEENTITY"); */
            
            
            
            //JasperParameter jasperParameter;
           // jasperParameter.put("IMAGEPATH", "http://" + "locahost:8080/is3102Web/report/merlionhomepage.png");
            
            ec.getExternalContext().setResponseContentType("application/pdf");
            OutputStream outputStream = ec.getExternalContext().getResponseOutputStream();
            
            JasperRunManager.runReportToPdfStream(reportStream, outputStream, new HashMap(), jrResultSetDataSource);
            
            outputStream.flush();
            outputStream.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}